package com.LeetCode.Tree;

import org.junit.Test;

public class diameterOfBinaryTree {
    int res = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return res ;
    }

    public int dfs(TreeNode root){
        if(root == null) return 0;

        int leftDepth = dfs(root.left);
        int rightDepth = dfs(root.right);

        //相当于遍历每个节点的最大直径
        res = Math.max(res , leftDepth + rightDepth);

        return Math.max(leftDepth ,rightDepth) + 1;
    }

    @Test
    public void test(){
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        System.out.println(diameterOfBinaryTree(node1));
    }
}
